﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP09S03_OfficeTracking.Controller;
using WP09S03_OfficeTracking.DAL;
using WP09S03_OfficeTracking.DAL.DSItemTableAdapters;

namespace WP09S03_OfficeTracking
{
    public partial class FrmReplenishTransaction : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        ItemController item = new ItemController();
        TransactionController tran = new TransactionController();
        private CategoryTableAdapter adpCate;
        public FrmReplenishTransaction()
        {
            InitializeComponent();
            adpCate = new CategoryTableAdapter();
            Init();
        }

        private void Init()
        {
            DataBinding_Category();
        }

        private void DataBinding_Category()
        {
            DSItem.CategoryDataTable table = new DSItem.CategoryDataTable();
            table = adpCate.GetDataByConsumable(1);
            this.cbbCategory.DataSource = table;
            this.cbbCategory.DisplayMember = table.CategoryNameColumn.ColumnName;
            this.cbbCategory.ValueMember = table.CategoryIDColumn.ColumnName;
        }

        private void DataBinding_ReplenishTransaction(string returnTranID, System.Nullable<System.DateTime> createDateFrom, System.Nullable<System.DateTime> createDateTo, string itemID, string itemName, string categoryID, string brand, string model)
        {
            DSTransaction.vw_ReplenishTransactionDataTable table = tran.GetReplenishTransaction(returnTranID, createDateFrom, createDateTo, itemID, itemName, categoryID, brand, model);
            table.Columns.Remove(table.CategoryIDColumn.ColumnName);
            this.dgvData.DataSource = table;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string returnTranID = this.txtTransactionID.Text.Trim();
                System.Nullable<System.DateTime> createDateFrom=null;
                System.Nullable<System.DateTime> createDateTo=null;
                if (this.cbSelectDate.Checked)
                {
                    createDateFrom = Convert.ToDateTime(this.dtpCreateDateFrom.Text);
                    createDateTo = Convert.ToDateTime(this.dtpCreateDateTo.Text);

                }
                string itemID = this.txtItemID.Text.Trim();
                string itemName = this.txtItemName.Text.Trim();
                string categoryID = this.cbbCategory.SelectedValue.ToString();
                string brand = this.txtBrand.Text.Trim();
                string model = this.txtModel.Text.Trim();
                DataBinding_ReplenishTransaction(returnTranID, createDateFrom, createDateTo, itemID, itemName, categoryID, brand, model);
            }
            catch (Exception err )
            {
                SetMessage("Search is fail." + err.Message);
                CatchError(err);
            }
        }

        private void cbSelectDate_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.cbSelectDate.Checked)
                {
                    this.dtpCreateDateFrom.Enabled = true;
                    this.dtpCreateDateTo.Enabled = true;
                }
                else
                {
                    this.dtpCreateDateFrom.Enabled = false;
                    this.dtpCreateDateTo.Enabled = false;
                }
            }
            catch (Exception err)
            {
                CatchError(err);
            }
        }
    }
}
